package com.mingmen.canting.zhenban;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.activity.BohuiYuanyinActivity;
import com.mingmen.canting.activity.LoginActivity;
import com.mingmen.canting.activity.YiShenHeActivity;
import com.mingmen.canting.adapter.BohuiListAdapter;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.definerecycleview.SwipeRecyclerView;
import com.mingmen.canting.defineview.ToastDialog;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.http.MyOkhttp;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.ZhenBanShenHe;
import com.mingmen.canting.refresh.ClassicsFooter;
import com.mingmen.canting.refresh.ClassicsHeader;
import com.mingmen.canting.util.DividerItemDecoration;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class BohuiListragment extends BaseFragment implements BohuiListAdapter.OnItemClickListener {

    @BindView(R.id.recyclerview)
    SwipeRecyclerView mRecyclerview;

    Unbinder unbinder;
    private String mTitle;
    //Fragment的View加载完毕的标记
    private boolean isLoading = false;

    //Fragment对用户可见的标记
    private boolean isUIVisible;


    private BohuiListAdapter bohuiListAdapter = null;
    private LinearLayoutManager mLinearLayoutManager;


    public static BohuiListragment getInstance(String title) {
        BohuiListragment sf = new BohuiListragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    ToastDialog toastDialog;
    String token;
    List<ZhenBanShenHe.DataBean> dataBeanList = new ArrayList<>();

    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;
    @BindView(R.id.btn_digndan)
    Button btn_digndan;
    @BindView(R.id.btn_suofang)
    Button btnSuofang;
    @BindView(R.id.img_list)
    ImageView imgList;

    public void getData() {
        HttpResponse.daiShenhe(token, "2", "", "", "1",1, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("BohuiBOhuio", s + "");
                BaseBean baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    ZhenBanShenHe zhenBanShenHe = new ZhenBanShenHe();
                    zhenBanShenHe = GsonUtil.GsonToBean(s, ZhenBanShenHe.class);
                    dataBeanList = zhenBanShenHe.getData();
                    totalPage=zhenBanShenHe.getTotalPage();
                    if (dataBeanList.size() == 0) {

                        layout_nodata.setVisibility(View.VISIBLE);
                    } else {
                        layout_nodata.setVisibility(View.GONE);
                    }
//                    dataBeanList.addAll(dataBeanList);
                    mRecyclerview.setAdapter(bohuiListAdapter);
                    bohuiListAdapter.setDataList(dataBeanList);
                }  else {
                    Toast.makeText(getActivity(), baseBean.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onAfter(String s, Exception e) {
                super.onAfter(s, e);
                refreshLayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
                refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                layout_nodata.setVisibility(View.VISIBLE);

            }
        });
    }

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    int pageNumber = 1;
    int totalPage;
    @Override
    public View initView() {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_bohui, null);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        unbinder = ButterKnife.bind(this, v);
        EventBus.getDefault().register(this);
        toastDialog = new ToastDialog(getActivity());
        toastDialog.setVerifyClickListener("该条订单正在被审核", new ToastDialog.VerifyClickListener() {
            @Override
            public void cancel() {

            }

            @Override
            public void send(String str) {

            }
        });
        bohuiListAdapter = new BohuiListAdapter(getActivity(), dataBeanList);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerview.setLayoutManager(mLinearLayoutManager);
        bohuiListAdapter.setOnItemClickListener(this);
        DividerItemDecoration itemDecorationHeader = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST);
        itemDecorationHeader.setDividerDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_main_bg_height_1));
        mRecyclerview.addItemDecoration(itemDecorationHeader);

        mRecyclerview.setAdapter(bohuiListAdapter);
        getData();
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                pageNumber = 1;
                getData();

            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if (pageNumber >= totalPage) {
                    Toast.makeText(getActivity(), "已到最后一页", Toast.LENGTH_SHORT).show();
                    refreshlayout.finishLoadMore(false);//传入false表示加载失败
                } else {
                    pageNumber++;
                    HttpResponse.daiShenhe(token, "2", "", "", "1",pageNumber, new StringCallback(getActivity()) {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            Log.d("BohuiBOhuio", s + "");
                            BaseBean baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                            if (baseBean.getCode() == 0) {
                                ZhenBanShenHe zhenBanShenHe = new ZhenBanShenHe();
                                zhenBanShenHe = GsonUtil.GsonToBean(s, ZhenBanShenHe.class);
//                                dataBeanList = zhenBanShenHe.getData();
                                totalPage=zhenBanShenHe.getTotalPage();
                                dataBeanList.addAll(zhenBanShenHe.getData());
                                if (dataBeanList.size() == 0) {

                                    layout_nodata.setVisibility(View.VISIBLE);
                                } else {
                                    layout_nodata.setVisibility(View.GONE);
                                }
//                    dataBeanList.addAll(dataBeanList);
                                mRecyclerview.setAdapter(bohuiListAdapter);
                                bohuiListAdapter.setDataList(dataBeanList);
                            }  else {
                                Toast.makeText(getActivity(), baseBean.getMessage(), Toast.LENGTH_SHORT).show();
                            }

                        }

                        @Override
                        public void onAfter(String s, Exception e) {
                            super.onAfter(s, e);
                            refreshLayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
                            refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);
                            layout_nodata.setVisibility(View.VISIBLE);
                            refreshLayout.finishRefresh(100/*,false*/);//传入false表示刷新失败
                            refreshLayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
                        }
                    });

                    refreshlayout.finishLoadMore(100/*,false*/);//传入false表示加载失败
                }


            }
        });

        return v;
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.recyclerview, R.id.btn_digndan, R.id.btn_suofang, R.id.img_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.recyclerview:
                break;
            case R.id.btn_suofang:
                btn_digndan.setVisibility(View.GONE);
                btnSuofang.setVisibility(View.VISIBLE);
                imgList.setVisibility(View.VISIBLE);
                break;
            case R.id.img_list:
//                startActivity(new Intent(getActivity(), DingDanActivity.class));

                imgList.setVisibility(View.GONE);
                btn_digndan.setVisibility(View.VISIBLE);
                btnSuofang.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_digndan:
                SharePreferenceUtil.put(getActivity(), "modify", "0");
                startActivity(new Intent(getActivity(), ZhenBanDingDanActivity.class));
//                startActivity(new Intent(getActivity(), DingDanActivity.class));
                break;
        }
    }
    Intent intent;

    @Override
    public void onItemClickListener(View view, int pos, BohuiListAdapter.ViewName viewName, List<ZhenBanShenHe.DataBean> myLiveLists) {

        switch (view.getId()) {

            case R.id.btn_zhuijia:
//                Toast.makeText(getActivity(), "追加" + pos, Toast.LENGTH_SHORT).show();

                SharePreferenceUtil.put(getActivity(), "modify", "2");
                SharePreferenceUtil.put(getActivity(), "applyId", dataBeanList.get(pos).getApplyId());
         /*       intent = new Intent(getActivity(), ZhenXiuGaiActivity.class);

                intent.putExtra("applyId", dataBeanList.get(pos).getApplyId());
                startActivity(intent);
*/
                HttpResponse.getCookLockStatus(token, "1", new StringCallback(getActivity()) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BaseBean baseBean=new BaseBean();
                        baseBean=GsonUtil.GsonToBean(s,BaseBean.class);
                        if (baseBean.getCode()==504){
                            toastDialog.show();
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    toastDialog.dismiss();
                                    intent = new Intent(getActivity(), ZhenBanDingDanActivity.class);
                                    startActivity(intent);
                                }
                            }, 2000);
                        }else if (baseBean.getCode()==0){
//                            intent = new Intent(getActivity(), ZhenXiuGaiActivity.class);
                            intent = new Intent(getActivity(), ZhenBanDingDanActivity.class);
                            intent.putExtra("applyId", dataBeanList.get(pos).getApplyId());
                            startActivity(intent);
                        }
                    }
                });

                break;
            case R.id.layout_bohui:

                intent = new Intent(getActivity(), BohuiYuanyinActivity.class);
                intent.putExtra("applyId", myLiveLists.get(pos).getApplyId());
                startActivity(intent);

                break;
            default:

                Intent intent2 = new Intent(getActivity(), YiShenHeActivity.class);
                intent2.putExtra("applyId", dataBeanList.get(pos).getApplyId());
//                applyTime
                intent2.putExtra("position", pos + "");
                intent2.putExtra("applyTime", dataBeanList.get(pos).getApplyTime());
                startActivity(intent2);
                break;
        }
    }

    public void onEventMainThread(Integer integer) {
        if (integer == 11) {
            getData();
        }

    }
}